#!/bin/bash
# date: 2025/04/24
# desc: install zookeeper-3.9.2-debian-12-r11 canal-admin-v1.1.8  canal-server-v1.1.7

# canal as mysql-slave, sync binlong to kafka/mq

# https://github.com/alibaba/canal/wiki/QuickStart
# https://github.com/alibaba/canal/blob/canal-1.1.8/charts/README.md

# helm version
# version.BuildInfo{Version:"v3.12.1", GitCommit:"f32a527a060157990e2aa86bf45010dfb3cc8b8d", GitTreeState:"clean", GoVersion:"go1.20.4"}

check_mysql_master() {
  mysql -h 127.0.0.1 -u root -p123456 -e "SHOW VARIABLES LIKE 'binlog_format'"
  # ; +---------------+-------+
  # ; | Variable_name | Value |
  # ; +---------------+-------+
  # ; | binlog_format | ROW   |
  # ; +---------------+-------+

  CREATE USER 'canal'@'%' IDENTIFIED BY 'canal'
  GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%'

  mysql -h 127.0.0.1 -u root -p123456 -e "SHOW SLAVE HOSTS;"
  mysql -h 127.0.0.1 -u root -p123456 -e "SHOW MASTER STATUS;"

  # CREATE TABLE `t_demo` (
  #     `id` int NOT NULL AUTO_INCREMENT,
  #     `user` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  # ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  # INSERT INTO t_demo(user) VALUES('a1')('a2')('a3')('a4')('a5');
  # DELETE FROM t_demo where id=1;
}

install_cvm() {
  wget -nc https://gh-proxy.com/https://github.com/alibaba/canal/releases/download/canal-1.1.7/canal.admin-1.1.7.tar.gz
  wget -nc https://gh-proxy.com/https://github.com/alibaba/canal/releases/download/canal-1.1.7/canal.deployer-1.1.7.tar.gz
  wget -nc https://gh-proxy.com/https://github.com/alibaba/canal/releases/download/canal-1.1.7/canal.adapter-1.1.7.tar.gz

  mkdir canaladmin && tar xf canal.admin-1.1.7.tar.gz -C canaladmin
  mkdir canaldeployer && tar xf canal.deployer-1.1.7.tar.gz -C canaldeployer
  mkdir canaladpter && tar xf canal.adapter-1.1.7.tar.gz -C canaladpter

  # vim canaldeployer/conf/canal.properties
  # use canal-admin config canal-server
  # vim canaldeployer/conf/canal-admin.properties
  sh canaldeploye/bin/stop.sh
  sh canaldeployer/bin/startup.sh

  # http://canaladmin:8089 admin 123456
  # vim canaladmin/conf/instance.properties
  sh canaladmin/bin/stop.sh
  sh canaladmin/bin/startup.sh
}

zookeeper() {
  # helm repo add bitnami https://charts.bitnami.com/bitnami
  # helm search repo zookeeper
  # helm pull bitnami/zookeeper
  # tar xf zookeeper-13.4.10.tgz
  kubectl create ns canal
  # docker pull m.daocloud.io/docker.io/bitnami/zookeeper:3.9.2-debian-12-r11
  # helm -n canal uninstall canal-zookeeper
  helm -n canal install canal-zookeeper zookeeper
}

# http://127.0.0.1:8089
canaladmin() {
  # https://github.com/alibaba/canal/tree/canal-1.1.8/charts
  # git clone https://gh-proxy.com/https://github.com/alibaba/canal.git

  mysql -h 127.0.0.1 -P 3306 -u root -p <118-canal_manager.sql
  mysql -h 127.0.0.1 -P 3306 -u root -p <117-create_tsdb.sql
  # docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/canal/canal-admin:v1.1.7
  # helm -n canal uninstall canal-admin
  helm -n canal canal-admin install canal-admin
  kubectl apply -f canal-admin/canal-admin-nodeport.yaml
}

canalserver() {
  # v1.1.7 k8s bug Failed to get D-Bus connection: Operation not permitted
  # https://github.com/alibaba/canal/issues/4911

  # docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/canal/canal-server:v1.1.8

  # vim values.yaml ### canal.admin.register.cluster = default
  # helm -n canal uninstall canal-server
  helm -n canal install canal-server canal-server
  helm install -f values-txmq-mall.yaml -n canal canal-server-txmq-mall canal-server
}

uninstall() {
  kubectl delete ns canal
}

canal_pods() {
  helm -n canal ls
  kubectl -n canal get pods
}

canal_log() {
  echo ">>> canal-server-0"
  kubectl -n canal exec -it canal-server-0 -- tail canal-server/logs/canal/canal.log
  echo ">>> canal-server-txmq-mall-0"
  kubectl -n canal exec -it canal-server-txmq-mall-0 -- tail canal-server/logs/canal/canal.log
}

case "$1" in
install)
  zookeeper
  canaladmin
  canalserver
  ;;
uninstall)
  uninstall
  ;;
ls)
  canal_pods
  ;;
log)
  canal_log
  ;;
*)
  echo "$0 install|uninstall|ls|log"
  ;;
esac
